﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.service.impl;
using cwtrock.model;
using cwtrock.util;
using cwtrock.view;

namespace cwtrock
{
    public partial class FrmLogin : Form
    {
        private SysUsrServiceImpl usrService = new SysUsrServiceImpl();

        public FrmLogin()
        {
            InitializeComponent();
            errorOK.Clear();
            errorCancel.Clear();
        }

        private void btnConcel_Click(object sender, EventArgs e)
        {
            DialogResult bb = MessageBox.Show("是否要退出登录？", "退出登录", MessageBoxButtons.YesNo);
            if (Convert.ToString(bb) == "Yes")
            { Application.Exit(); }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SysUsr usr = new SysUsr();
            usr.Usrid = this.txtUser.Text;
            usr.Usrpwd = this.txtPasword.Text;
         
            bool b1 = validaing(errorOK, txtUser, 20, "用户名");
            bool b2 = validaing(errorCancel, txtPasword, 5, "密码");

            if (!(b1 && b2))
            {
                return;
            }

            usr = usrService.login(usr);

            if (usr == null)
            {
                MessageBox.Show("数据库没有打开", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;

            }

            if ((usr.Usrname==null) ||("".Equals(usr.Usrname)))
            {
                DialogResult a = MessageBox.Show("用户名或密码错误!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtUser.Text = "";
                txtPasword.Text = "";
                txtUser.Focus();
                return;
            }

            else 
            {
                AppContextProvider.instance.LogUser = usr;
              //  FrmMain main = new FrmMain(usr);
                FrmMain main = FrmMain.getInstance();
                main.Show();
                this.Hide();
                
            }

           
           // .
        }


        /// <summary>
        /// 验证输入框的输入内容
        /// </summary>
        /// <param name="textbox">输入框控件</param>
        /// <param name="length">最大输入长度</param>
        /// <param name="controlName">输入框的名称</param>
        /// <returns>true验证通过，false验证不通过</returns>
        private bool validaing(ErrorProvider errorProvider1, TextBox textbox, int length, string controlName)
        {
            bool result = false;
            if (textbox.TextLength == 0)
            {
                errorProvider1.SetError(textbox,  controlName + "不允许为空！");
                textbox.SelectAll();
                textbox.Focus();

            }
            else if (textbox.TextLength > length)
            {
                errorProvider1.SetError(textbox, "请输入小于" + length.ToString() + "个字符的" + controlName + "！");
                textbox.SelectAll();
                textbox.Focus();
            }
            else
            {
                errorProvider1.SetError(textbox, "");
                result = true;
            }
            return result;

        }

    }
}
